﻿#include "pdfimageprovider.h"

PdfImageProvider::PdfImageProvider(QObject *parent)
    : QQuickImageProvider{QQuickImageProvider::Image}
{
    image = QImage(200, 200, QImage::Format_ARGB32);
    image.fill(QColor(0, 0, 0));
}

QImage PdfImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
    // 若 url 为 "image://provider/imageTag/0"，那么 id 就是 "imageTag/0"
    if (id.startsWith("imageTag")) {
        qDebug() << "---------------1";
        if (size) {
            qDebug() << "---------------" << size->width() << ":" << size->height();
            *size = image.size();
            qDebug() << "---------------" << size->width() << ":" << size->height();
        }
        return image;
    } else if (id.startsWith("item1")) {
        QStringList sl = id.split("/");
        qDebug() << "---------------" << sl[1].toInt() << " " << sl[2].toInt();
        return QImage("clip.png");
    }
    return QImage();
}

void PdfImageProvider::updateImage(QImage &img)
{
    this->image = img;
    emit imageChanged("imageTag");
}

void PdfImageProvider::clipImage(double x, double y, int len)
{
    QRect rect(x, y, len, len);
    QImage clip = this->image.copy(rect);
    clip.save("clip.png", "PNG", 100);
}
